# -*- coding: utf-8 -*-
import hashlib
import requests
import configparser
import os
from qa_tester import settings
import json


def md5(s):
    m = hashlib.md5()
    m.update(s)
    return m.hexdigest()


def lesson_plan(username, password, interface_name):
    header_login = {
        'Accept': 'application/json, text/javascript, */*; q=0.01',
        'Accept-Encoding': 'gzip, deflate, sdch',
        'Accept-Language': 'zh-CN,zh;q=0.8',
        'Connection': 'keep-alive',
        'Host': 'test-lms3.hfjy.com',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
        'X-Requested-With': 'XMLHttpRequest'
    }
    data = {
        'username': username,
        'password': md5(md5(username) + md5(password))
    }
    req = requests.post('http://test-lms3.hfjy.com:1080/user/login', headers=header_login, data=data)
    # r = requests.post(interface_name, headers=header_login, data=data)

    token = eval(req.content)['data']['access_token']
    header_main = {
        'Accept': 'application/json, text/javascript, */*; q=0.01',
        'Accept-Encoding': 'gzip, deflate, sdch',
        'Accept-Language': 'zh-CN,zh;q=0.8',
        'Connection': 'keep-alive',
        'Authorization': token,
        'Host': 'test-lms3.hfjy.com',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
        'X-Requested-With': 'XMLHttpRequest'
    }
    r = requests.post(interface_name, headers=header_main)
    res = r.content.decode('utf-8')
    return eval(res)


def config_parser(directory_name, filename, select, key):
    file_path = os.path.join(settings.BASE_DIR, directory_name, filename)
    cf = configparser.ConfigParser()
    cf.read(file_path, encoding='utf-8')
    code, message = cf[select][key].strip("'").split('_')
    return {'code': code, 'message': message}


def parse_txt(directory_name, filename):
    file_path = os.path.join(settings.BASE_DIR, directory_name, filename)
    file_list = open(file_path, 'r')
    lines = file_list.readlines()
    file_list.close()
    return lines


def get_zt_project(zt_product_id):
    header_login = {
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Encoding': 'gzip, deflate, sdch',
        'Accept-Language': 'zh-CN,zh;q=0.8',
        'Connection': 'keep-alive',
        'Host': 'zt.hfjy.com',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
    }
    data = {
        'account': 'wujianlong',
        'password': md5('123456hfjy')
    }
    req = requests.post('http://zt.hfjy.com/index.php?m=user&f=login&t=json', headers=header_login, data=data)
    cookie=req.cookies.values()[1]

    header_main = {
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Encoding': 'gzip, deflate, sdch',
        'Accept-Language': 'zh-CN,zh;q=0.8',
        'Connection': 'keep-alive',
        'Host': 'zt.hfjy.com',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
        'Cookie':'sid='+cookie
    }

    s = \
        eval(requests.get(
            'http://zt.hfjy.com/index.php?m=product&f=project&t=json&status=all&productID=' + zt_product_id,
            headers=header_main).content)['data']
    s = json.loads(s)
    return s['projectStats']
